import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'

Vue.use(VueRouter)
const routes = [{
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      showTab: true,
    }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About/About.vue'),
    meta: {
      showTab: true,
    }
  },
  {
    path: '/study',
    name: 'Study',
    component: () => import('../views/Study/Study.vue'),
    meta: {
      showTab: true,
    }
  },
  {
    path: '/text',
    name: 'Text',
    component: () => import('../views/Text/Text.vue'),
    meta: {
      showTab: true,
    }
  },
  {
    path: '/main',
    name: 'Main',
    component: () => import('../views/Main/Main.vue'),
    meta: {
      showTab: true,
      requireAuth: true
    }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/About/Search.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('../views/Home/Detail.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/info',
    name: 'Info',
    component: () => import('../views/Info.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/loginmm',
    name: 'Loginmm',
    component: () => import('../views/Main/Loginmm.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/loginyzm',
    name: 'Loginyzm',
    component: () => import('../views/Main/Loginyzm.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/mymsg',
    name: 'Mymsg',
    component: () => import('../views/Main/Mymsg.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/nickname',
    name: 'Nickname',
    component: () => import('../views/Main/Nickname.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/teachdetail',
    name: 'Teachdetail',
    component: () => import('../views/Home/TeachDetail.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/textdd',
    name: 'Textdd',
    component: () => import('../views/Main/Textdd.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/textvip',
    name: 'Textvip',
    component: () => import('../views/Main/Textvip.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/textyue',
    name: 'Textyue',
    component: () => import('../views/Main/Textyue.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/mylike',
    name: 'Mylike',
    component: () => import('../views/Main/Mylike.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/myteacher',
    name: 'Myteacher',
    component: () => import('../views/Main/Myteacher.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/yueke',
    name: 'Yueke',
    component: () => import('../views/Main/Yueke.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/yueke2',
    name: 'Yueke2',
    component: () => import('../views/Main/Yueke2.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/mystudy',
    name: 'Mystudy',
    component: () => import('../views/Main/Mystudy.vue'),
    meta: {
      showTab: false,
    }
  },
  {
    path: '/date',
    name: 'Date',
    component: () => import('../views/Main/Date.vue'),
    meta: {
      showTab: false,
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限
    if (!sessionStorage.getItem('token') && !localStorage.getItem('token')) {
      next({
        path: '/loginmm',
        query: {
          redirect: '/loginmm'
        } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
      // next('/loginmm')
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router